package com.souku.finance.application.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.ViewPagerAdapter;
import com.souku.finance.application.base.ActivityManage;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RegisterBean;
import com.souku.finance.application.ui.MainActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.ui.mine.ExperienceActivity;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.ViewUtil;
import com.souku.finance.application.widget.MyViewPage;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.Success;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Android on 2016/12/7.
 * 要丢弃
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected MyViewPage idVp;
    protected TextView tvTitle1;
    protected EditText etMobile;
    protected EditText etReadName;
    protected EditText etIdNo;
    protected ImageView ivClear1;
    protected Button btnNext1;
    protected TextView tvTitle2;
    protected EditText etCode;
    protected Button btnCode2;
    protected Button btnNext2;
    protected TextView tvTitle3;
    protected EditText etPass;
    protected ImageView ivClear3;
    protected ImageView ivPass3;
    protected Button btnNext3;
    protected TextView topTv1;
    protected TextView topTv2;
    protected TextView topTv3;
    protected TextView topTv4;
    protected TextView topTv5;
    protected TextView topTv6;
    protected Button btnNext4;
    protected Button btnNext5;
    protected Button btnNext6;
    private View view1;
    private View view2;
    private View view3;
    private View view4;
    private View view5;
    private View view6;
    private String codeChecked = "";
    private String codeToken = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register);

        view1 = LayoutInflater.from(this).inflate(R.layout.page_register1, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.page_register2, null);
        view3 = LayoutInflater.from(this).inflate(R.layout.page_register3, null);
        view5 = LayoutInflater.from(this).inflate(R.layout.page_register5, null);
        view6 = LayoutInflater.from(this).inflate(R.layout.page_register6, null);
        view4 = LayoutInflater.from(this).inflate(R.layout.page_register4, null);
        initView();
        final ArrayList<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view5);
        views.add(view6);
        views.add(view4);
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        idVp.setAdapter(adapter);
        idVp.setOnPageChangeListener(this);

        ViewUtil.setDelete(etMobile, ivClear1, null);
        ViewUtil.setDelete(etPass, ivClear3, ivPass3);
        ViewUtil.setCountDown(btnCode2, this);
    }

    @Override
    public void internet() {

    }

    @Override
    public void onClick(View view) {

        String mobile = etMobile.getText().toString().trim();
        String pass = etPass.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        String realName = etReadName.getText().toString();
        String idNo = etIdNo.getText().toString().trim();

        if (view.getId() == R.id.top_back) {
            finishHelp();
        } else if (view.getId() == R.id.btn_next1) {
            if (mobile.isEmpty()) {
                ToastUtil.show("请输入手机号");
                return;
            }
            if (!MyTextUtil.isTelephone(mobile)) {
                ToastUtil.show("请输入正确的手机号");
                return;
            }
            isRegister(mobile);
        } else if (view.getId() == R.id.btn_next2) {
            if (mobile.equals(codeChecked)) {
                idVp.setCurrentItem(2);
                return;
            }
            if (code.isEmpty()) {
                ToastUtil.show("请输入验证码");
                return;
            }
            checkCode(mobile, code);
        } else if (view.getId() == R.id.btn_next3) {
            if (pass.isEmpty()) {
                ToastUtil.show("请输入密码");
                return;
            }
            if (pass.length() > 20 || pass.length() < 8) {
                ToastUtil.show("密码长度8-20位");
                return;
            }
            idVp.setCurrentItem(3);
        } else if (view.getId() == R.id.btn_next5) {
            if (realName.isEmpty()) {
                ToastUtil.show("请输入姓名");
                return;
            }
            idVp.setCurrentItem(4);
        } else if (view.getId() == R.id.btn_next6) {
            if (idNo.isEmpty()) {
                ToastUtil.show("请输入身份证号码");
                return;
            }
            register(mobile, pass, realName, idNo);
        } else if (view.getId() == R.id.btn_next4) {
            Intent intent = new Intent(this, ExperienceActivity.class);
            startActivity(intent);
            ActivityManage.finishOthers(ExperienceActivity.class);
        } else if (view.getId() == R.id.btn_code2) {
            sendCode(mobile);
        }

    }

    @Override
    public void onBackPressed() {
        finishHelp();
    }

    private void isRegister(String mobile) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        RequestManager.getInstance().post(UrlConstact.IS_REGISTER, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    idVp.setCurrentItem(1);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void sendCode(String mobile) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        RequestManager.getInstance().post(UrlConstact.SEND_MOBILE_CODE, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    ToastUtil.show("验证码发送成功");
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void checkCode(final String mobile, String code) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        map.put("code", code);
        RequestManager.getInstance().post(UrlConstact.VALIDATE_MOBILE_CODE, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    codeChecked = mobile;
//                    codeToken = JsonUtil.getString(JsonUtil.getData(response), "verifyToken");
                    idVp.setCurrentItem(2);
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }

    private void register(String mobile, String password, String realName, String idNo) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        map.put("userPwd", password);
        map.put("realName", realName);
        map.put("idNo", idNo);
        map.put("sourceType", "1");
        map.put("sourceFrom", "APP");
        map.put("referer", "");
//        map.put("verifyToken", codeToken);
        map.put("pid", "");
        map.put("weiXinOpenId", "");
        RequestManager.getInstance().post(UrlConstact.REGISTER, ParamUtil.getSing(map), new ReqListener(this, new Success() {
            @Override
            public void onSuccess(String response) {
                if (JsonUtil.getCode(response) == 1) {
                    RegisterBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), RegisterBean.class);
                    ShareUtil.getInstance()
                            .putExtra(Constant.IsRegistered, true)
                            .putExtra(Constant.Mobile, bean.getMobile())
                            .putExtra(Constant.Token, bean.getOauthToken())
                            .putExtra(Constant.UserId, bean.getId())
                            .putExtra(Constant.AvatarUrl, bean.getAvatarUrl())
                            .putExtra(Constant.IsNewHand, true)
                            .putExtra(Constant.IsReal, false)
                            .putExtra(Constant.IsRole, false)
                            .putExtra(Constant.IsSetPayPwd, false)
                            .putExtra(Constant.IsBind, false)
                            .putExtra(Constant.IsBorrower, "1".equals(bean.getIsBorrower()))
                            .putExtra(Constant.IsLogin, true);
                    idVp.setCurrentItem(5);
                    MainActivity.page = 2;
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }
        }), ParamUtil.getHeaders());
    }


    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(RegisterActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        idVp = (MyViewPage) findViewById(R.id.id_vp);
        tvTitle1 = (TextView) view1.findViewById(R.id.tv_title1);
        etMobile = (EditText) view1.findViewById(R.id.et_mobile);
        etReadName = (EditText) view5.findViewById(R.id.et_readName);
        etIdNo = (EditText) view6.findViewById(R.id.et_id_no);
        ivClear1 = (ImageView) view1.findViewById(R.id.iv_clear1);
        btnNext1 = (Button) view1.findViewById(R.id.btn_next1);
        btnNext1.setOnClickListener(RegisterActivity.this);
        tvTitle2 = (TextView) view2.findViewById(R.id.tv_title2);
        etCode = (EditText) view2.findViewById(R.id.et_code);
        btnCode2 = (Button) view2.findViewById(R.id.btn_code2);
        btnNext2 = (Button) view2.findViewById(R.id.btn_next2);
        btnNext2.setOnClickListener(RegisterActivity.this);
        tvTitle3 = (TextView) view3.findViewById(R.id.tv_title3);
        etPass = (EditText) view3.findViewById(R.id.et_pass);
        ivClear3 = (ImageView) view3.findViewById(R.id.iv_clear3);
        ivPass3 = (ImageView) view3.findViewById(R.id.iv_pass3);
        btnNext3 = (Button) view3.findViewById(R.id.btn_next3);
        btnNext3.setOnClickListener(RegisterActivity.this);
        topTv1 = (TextView) findViewById(R.id.top_tv1);
        topTv2 = (TextView) findViewById(R.id.top_tv2);
        topTv3 = (TextView) findViewById(R.id.top_tv3);
        topTv4 = (TextView) findViewById(R.id.top_tv4);
        topTv5 = (TextView) findViewById(R.id.top_tv5);
        topTv6 = (TextView) findViewById(R.id.top_tv6);
        btnNext4 = (Button) view4.findViewById(R.id.btn_next4);
        btnNext4.setOnClickListener(RegisterActivity.this);
        btnNext5 = (Button) view5.findViewById(R.id.btn_next5);
        btnNext5.setOnClickListener(RegisterActivity.this);
        btnNext6 = (Button) view6.findViewById(R.id.btn_next6);
        btnNext6.setOnClickListener(RegisterActivity.this);
    }

    private void finishHelp() {
        if (idVp.getCurrentItem() == 0) {
            finish();
        } else if (idVp.getCurrentItem() == 3) {
            ActivityManage.exit();
        } else {
            idVp.setCurrentItem(idVp.getCurrentItem() - 1);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setTop(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private void setTop(int i) {
        switch (i) {
            case 0:
                topTv1.setTextColor(getResources().getColor(R.color.main));
                topTv1.setBackgroundResource(R.drawable.stoke_main_round);
                topTv2.setTextColor(getResources().getColor(R.color.gray_text));
                topTv2.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv3.setTextColor(getResources().getColor(R.color.gray_text));
                topTv3.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv4.setTextColor(getResources().getColor(R.color.gray_text));
                topTv4.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_gray_round);
                break;
            case 1:
                topTv1.setTextColor(getResources().getColor(R.color.gray_text));
                topTv1.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv2.setTextColor(getResources().getColor(R.color.main));
                topTv2.setBackgroundResource(R.drawable.stoke_main_round);
                topTv3.setTextColor(getResources().getColor(R.color.gray_text));
                topTv3.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv4.setTextColor(getResources().getColor(R.color.gray_text));
                topTv4.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_gray_round);
                break;
            case 2:
                topTv1.setTextColor(getResources().getColor(R.color.gray_text));
                topTv1.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv2.setTextColor(getResources().getColor(R.color.gray_text));
                topTv2.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv3.setTextColor(getResources().getColor(R.color.main));
                topTv3.setBackgroundResource(R.drawable.stoke_main_round);
                topTv4.setTextColor(getResources().getColor(R.color.gray_text));
                topTv4.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_gray_round);
                break;
            case 3:
                topTv1.setTextColor(getResources().getColor(R.color.gray_text));
                topTv1.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv2.setTextColor(getResources().getColor(R.color.gray_text));
                topTv2.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv3.setTextColor(getResources().getColor(R.color.gray_text));
                topTv3.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv4.setTextColor(getResources().getColor(R.color.main));
                topTv4.setBackgroundResource(R.drawable.stoke_main_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_gray_round);
                break;
            case 4:
                topTv1.setTextColor(getResources().getColor(R.color.gray_text));
                topTv1.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv2.setTextColor(getResources().getColor(R.color.gray_text));
                topTv2.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv3.setTextColor(getResources().getColor(R.color.gray_text));
                topTv3.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv4.setTextColor(getResources().getColor(R.color.main));
                topTv4.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_main_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_gray_round);
                break;
            case 5:
                topTv1.setTextColor(getResources().getColor(R.color.gray_text));
                topTv1.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv2.setTextColor(getResources().getColor(R.color.gray_text));
                topTv2.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv3.setTextColor(getResources().getColor(R.color.gray_text));
                topTv3.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv4.setTextColor(getResources().getColor(R.color.main));
                topTv4.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv5.setTextColor(getResources().getColor(R.color.gray_text));
                topTv5.setBackgroundResource(R.drawable.stoke_gray_round);
                topTv6.setTextColor(getResources().getColor(R.color.gray_text));
                topTv6.setBackgroundResource(R.drawable.stoke_main_round);
                break;
        }
    }
}
